package com.theatre.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.theatre.userservice.mapper.ScriptCommentMapper;
import com.theatre.userservice.model.entity.ScriptComment;
import com.theatre.userservice.model.entity.Shop;
import com.theatre.userservice.model.vo.ScriptCommentVO;
import com.theatre.userservice.service.ScriptCommentService;
import com.theatre.userservice.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScriptCommentServiceImpl implements ScriptCommentService {

    @Autowired
    private ScriptCommentMapper commentMapper;

    @Autowired
    private ShopService shopService;

    @Override
    public boolean addComment(ScriptComment comment) {
        return commentMapper.insert(comment) > 0;
    }

    @Override
    public List<ScriptCommentVO> listCommentsByScriptId(Long scriptId) {
        List<ScriptComment> comments = commentMapper.selectList(
                new LambdaQueryWrapper<ScriptComment>()
                        .eq(ScriptComment::getScriptId, scriptId)
                        .orderByDesc(ScriptComment::getCreatedTime)
        );

        List<ScriptCommentVO> voList = new ArrayList<>();
        for (ScriptComment c : comments) {
            ScriptCommentVO vo = new ScriptCommentVO();
            vo.setId(c.getId());
            vo.setScriptId(c.getScriptId());
            vo.setShopId(c.getShopId());
            Shop shop = shopService.getById(c.getShopId());
            vo.setShopName(shop != null ? shop.getName() : "未知商铺");
            vo.setContent(c.getContent());
            voList.add(vo);
        }
        return voList;
    }
}
